package org.finmanager.statistics;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

/**
 * Created by Kelvin on 28/4/2014.
 */

//A class to control the bar chart by getting CategoryStackedBarChartDataSet & categoryStackedBarChartUI
public class CategoryStackedBarChartController implements ActionListener {
	public static final String INCREASE_MONTH = "CategoryStackedBarChartController.INCREASE_MONTH";
	public static final String DECREASE_MONTH = "CategoryStackedBarChartController.DECREASE_MONTH";
	public static final String SET_CURRENT_MONTH = "CategoryStackedBarChartController.SET_CURRENT_MONTH";

	private final CategoryStackedBarChartDataSet categoryStackedBarChartDataSet;
	private final CategoryStackedBarChartUI categoryStackedBarChartUI;

	public CategoryStackedBarChartController(StatisticsController statisticsController) {
		this.categoryStackedBarChartDataSet = statisticsController.getStatisticsModel().getCategoryStackedBarChartDataSet();
		this.categoryStackedBarChartUI = new CategoryStackedBarChartUI(this);

		categoryStackedBarChartUI.addButtonListener(this);

		statisticsController.addComponent(getUIMainComponent(), "Proportion - Time");
	}

	public CategoryStackedBarChartDataSet getCategoryStackedBarChartDataSet() {
		return categoryStackedBarChartDataSet;
	}

	public JComponent getUIMainComponent() {
		return categoryStackedBarChartUI.getMainComponent();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String ac = e.getActionCommand();
		if (ac.equals(INCREASE_MONTH)) {
			categoryStackedBarChartDataSet.increaseMonth();
			categoryStackedBarChartUI.setCurrentMonthText(categoryStackedBarChartDataSet.getCalendar());
		}
		else if (ac.equals(DECREASE_MONTH)) {
			categoryStackedBarChartDataSet.decreaseMonth();
			categoryStackedBarChartUI.setCurrentMonthText(categoryStackedBarChartDataSet.getCalendar());
		}
		else if (ac.equals(SET_CURRENT_MONTH)) {
			categoryStackedBarChartDataSet.setCalendar(Calendar.getInstance());
			categoryStackedBarChartUI.setCurrentMonthText(Calendar.getInstance());
		}
	}
}
